#include "Texture.h"


Texture::Texture(LPDIRECT3DDEVICE9 D3ddv, LPWSTR FileName, int Cols, int Rows, int Count, D3DCOLOR Transcolor)
{
	this->d3ddv = D3ddv;
	this->fileName = FileName;
	this->transcolor = Transcolor;
	this->texture = NULL;

	this->_Cols = Cols;
	this->_Rows = Rows;
	this->_Count = Count;

	this->Load();
}

Texture::~Texture()
{
	if (texture)
	{
		texture->Release();
	}
}

LPDIRECT3DTEXTURE9 Texture::Get_Texture()
{
	//if (texture != NULL)
			 
	return texture;
}

void Texture::Load()
{
	D3DXIMAGE_INFO info; 
	HRESULT result;

	result = D3DXGetImageInfoFromFile(fileName, &info);

	_Width = info.Width / _Cols;
	_Height = info.Height / _Rows;

	if (result != D3D_OK)
	{
		trace(L"[ERROR] Failed to get information from image file '%s'", fileName);
		return;
	}

	result = D3DXCreateTextureFromFileEx(
		d3ddv,
		fileName,
		info.Width,
		info.Height,
		1,
		D3DUSAGE_DYNAMIC,
		D3DFMT_UNKNOWN,
		D3DPOOL_DEFAULT,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		transcolor,//D3DCOLOR_XRGB(255, 255, 255),
		&info,
		NULL,
		&texture
		);

	if (result != D3D_OK) 
	{
		trace(L"[ERROR] Failed to create texture from file '%s'", fileName);
		return;
	}
}